/**
 * 会造成死锁的线程类
 * @author 杨亚旭
 * @date 2019-02-13-14:39
 **/
public class DeadLockThread implements Runnable {

    public int flag=1;
    private static Object o1=new Object(), o2=new Object();


    public void run() {
        System.out.println("flag="+flag);
        if(flag==1){
            synchronized (o1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){
                    System.out.println(1);
                }
            }
        }
        if(flag==0){
            synchronized (o2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1){
                    System.out.println(0);
                }
            }
        }
    }

    public static void main(String[] args) {

        DeadLockThread t1=new DeadLockThread();
        DeadLockThread t2=new DeadLockThread();
        t1.flag = 1;
        t2.flag = 0;
        new Thread(t1).start();
        new Thread(t2).start();
    }
}
